package com.rockcent.wechat.helper;

import com.rockcent.common.common.BusinessException;
import com.rockcent.common.utils.RDigestUtils;
import com.rockcent.mall.lib.dto.IntegrationCouponReqDto;
import com.rockcent.wechat.controller.CustomerCashCouponController;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.MessageFormat;

/**
 * Created by zhangjh on 2016/12/31.
 */
public enum IntegrationCouponHelper {
    SINGLETON;
    public String paramOfSyncMethod = "paramOfSync";
    public String paramOfExchangeMethod = "paramOfExchange";
    String paramOfSync = "appKey={0}&nonceStr={1}&timeStamp={2}&type={3}";
    String paramOfExchange = "appKey={0}&exchangeCode={1}&exchangeNum={2}&nonceStr={3}&timeStamp={4}&type={5}";
    public Logger log = LoggerFactory.getLogger(IntegrationCouponHelper.class);

    /**
     * @param reqDto
     * @param method
     */
    private void validateSign(IntegrationCouponReqDto reqDto, String method) {
        if (method.equals(paramOfSyncMethod)) {
            String stringA = MessageFormat.format(paramOfSync, reqDto.getAppKey(), reqDto.getNonceStr(), reqDto.getTimeStamp(), reqDto.getType());
            log.info("======================>传入的stringA：" + stringA);
            String sign = RDigestUtils.md5Hex(stringA).toUpperCase();
            if (!sign.equals(reqDto.getSign())) {
                log.info("======================>传入的sign：" + reqDto.getSign());
                log.info("======================>加密的sign：" + sign);
                throw new BusinessException("签名不正确");
            }

        } else if (method.equals(paramOfExchangeMethod)) {

            String stringA = MessageFormat.format(paramOfExchange, reqDto.getAppKey(), reqDto.getExchangeCode(), reqDto.getExchangeNum(), reqDto.getNonceStr(), reqDto.getTimeStamp(), reqDto.getType());
            log.info("======================>传入的stringA：" + stringA);
            String sign = RDigestUtils.md5Hex(stringA).toUpperCase();
            if (!sign.equals(reqDto.getSign())) {
                log.info("======================>传入的sign：" + reqDto.getSign());
                log.info("======================>加密的sign：" + sign);
                throw new BusinessException("签名不正确");
            }

        }


    }

    /**
     * 校验请求参数
     *
     * @param reqDto
     */
    public void validate(IntegrationCouponReqDto reqDto, String method) {

        if (reqDto == null) {
            throw new BusinessException("参数为空");
        }
        log.info("======================>reqDto.getType()====》" + reqDto.getType());
        if (StringUtils.isBlank(reqDto.getType()) || (!reqDto.getType().equals("coupon") && !reqDto.getType().equals("cash"))) {
            throw new BusinessException("类型错误");
        }
        String appKey = reqDto.getAppKey();
        String timeStamp = reqDto.getTimeStamp();
        String nonceStr = reqDto.getNonceStr();
        String signType = reqDto.getSignType();
        String sign = reqDto.getSign();

        String type = reqDto.getType();
        if (StringUtils.isBlank(appKey) || StringUtils.isBlank(timeStamp) ||
                StringUtils.isBlank(nonceStr) || StringUtils.isBlank(signType) || StringUtils.isBlank(sign)
                || StringUtils.isBlank(type)) {
            throw new BusinessException("部分参数为空");
        }

        if (!"MD5".equals(signType)) {
            throw new BusinessException("签名类型只支持MD5");
        }


        validateSign(reqDto, method);


    }

    public void validate(String accessToken) {

    }
}
